package com.java110.mall.goods.bmo.productStock.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.dto.product.ProductStockDto;
import com.java110.mall.goods.bmo.productStock.IGetProductStockBMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.intf.goods.IProductStockInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getProductStockBMOImpl")
public class GetProductStockBMOImpl implements IGetProductStockBMO {

    @Autowired
    private IProductStockInnerServiceSMO productStockInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     * @param productStockDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ProductStockDto productStockDto) {


        int count = productStockInnerServiceSMOImpl.queryProductStocksCount(productStockDto);

        List<ProductStockDto> productStockDtos = null;
        if (count > 0) {
            productStockDtos = productStockInnerServiceSMOImpl.queryProductStocks(productStockDto);
        } else {
            productStockDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) productStockDto.getRow()), count, productStockDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 收银台搜索
     *
     * @param productStockDto
     * @return
     */
    @Override
    public ResponseEntity<String> search(ProductStockDto productStockDto) {
        /*List<ProductStockDto> productStockDtos = productStockInnerServiceSMOImpl.queryProductStocks(productStockDto);
        List<ProductDto> productDtos = null;
        if (productStockDtos != null && productStockDtos.size() > 0) {
            productDtos = BeanConvertUtil.covertBeanList(productStockDtos, ProductDto.class);
            return ResultVo.createResponseEntity(productDtos);
        }*/
        //productDtos = productInnerServiceSMOImpl.queryProducts(productDto);

        // todo 先用 barCode 去优先搜索，如果搜索不到 则用名称再去搜索
        List<ProductSpecValueDto> productSpecValueDtos = null;
        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setBarCode(productStockDto.getBarCode());
        if (productSpecValueDto.getBarCode().length() == 4) {
            productSpecValueDto.setBarCodeLike(productSpecValueDto.getBarCode());
            productSpecValueDto.setBarCode("");
        }
        productSpecValueDto.setShopId(productStockDto.getShopId());
        productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);

        // todo 如果商品为空则用名称搜索
        if(productSpecValueDtos == null || productSpecValueDtos.size()< 1){
            productSpecValueDto = new ProductSpecValueDto();
            productSpecValueDto.setProdName(productStockDto.getBarCode());
            productSpecValueDto.setShopId(productStockDto.getShopId());
            productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
        }

        List<ProductDto> productDtos = freshProductDtos(productSpecValueDtos);
        return ResultVo.createResponseEntity(productDtos);
    }

    private List<ProductDto> freshProductDtos(List<ProductSpecValueDto> productSpecValueDtos) {
        List<ProductDto> productDtos = null;
        if (productSpecValueDtos == null || productSpecValueDtos.size() < 1) {
            return productDtos;
        }
        //输入
        List<String> productIds = new ArrayList<>();

        for (ProductSpecValueDto tmpProductDto : productSpecValueDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        productDtos = new ArrayList();
        for (ProductSpecValueDto tmpProductValueDto : productSpecValueDtos) {
            ProductDto  productDto = BeanConvertUtil.covertBean( tmpProductValueDto, ProductDto.class );
            ProductSpecDto  productSpecDto = BeanConvertUtil.covertBean( tmpProductValueDto, ProductSpecDto.class );
            productDto.setProductSpec( productSpecDto );
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpProductValueDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    url = imgUrl + tmpFileRelDto.getFileRealName();
                    productDto.setCoverPhoto(url);
                }
            }
            productDtos.add( productDto );
        }

        return productDtos;
    }
}
